package com.gongzhiyun.springboot3test.base;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@Data
@Schema(description = "返回结果")
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "状态码",example = "500")
    private int code;
    @Schema(description = "应答结果是否成功",example = "false")
    private boolean success;
    @Schema(description = "应答结果信息",example = "操作成功")
    private String message;
    @Schema(description = "token信息",example = "1344359d-320b-4018-9b30-5d1a6752ef8c")
    private String token;
    private PageBean pageBean;
    @Schema(description = "响应数据")
    private T data;


    public static final int SUCCESS_CODE = 200;
    public static final int ERROR_CODE = 500;

    public void setInfo(int code, boolean success, String message, T data) {
        this.code = code;
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public static <T> R<T> ok(String message, T data) {
        R<T> r = new R<T>();
        r.setInfo(SUCCESS_CODE, true, message, data);
        return r;
    }

    public static <T> R<T> ok(String message) {
        return R.ok(message, null);
    }

    public static <T> R<T> ok(T data) {
        return R.ok("", data);
    }

    public static <T> R<T> fail(String message, T data) {
        R<T> r = new R<T>();
        r.setInfo(ERROR_CODE, false, message, data);
        return r;
    }

    public static <T> R<T> fail(String message) {
        return R.fail(message, null);
    }

    public static <T> R<T> fail(int code, String message) {
        R<T> r = new R<T>();
        r.setInfo(code, false, message, null);
        return r;
    }

}
